<?php
/**
 * 产品提货订单
 * User: bgl
 * Date: 2023/2/2
 * Time: 18:17
 */

namespace App\Admin\Controllers\ProductPickup;


use App\Admin\Ajax\ProductPickup\PickupAjax;
use App\Admin\Ajax\ProductPickup\PickupOrderAjax;
use App\Admin\Extensions\Grid\Tool\Export;
use App\Models\Basics\SettlementMethod;
use App\Models\Inventory\StorageDetail;
use App\Models\Product\ProductClassify;
use App\Models\ProductPickup\PickupOrder;
use App\Models\Purchase\PurchaseOrder;
use App\Models\Sys\Logs;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Request;

class PickupOrderController extends AdminController{

    protected $title = '产品提货（新）';

    protected function grid(){

        $grid = new Grid(new PickupOrder());
        $grid->column('pickup_no', '提货单号')->totalRow('汇总');
        $grid->column('skuClassify.title', '品类');
        $grid->column('skuClassify.season', '季节性')->using(ProductClassify::$_season);
        $grid->column('操作')->view('product-pickup.pickup-order.action');

        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->where(function ($query) {
                    $query->whereHas('sku', function ($query) {
                        $query->where('psku_code', 'like', "%{$this->input}%")
                            ->orWhere('psku_cn', 'like', "%{$this->input}%");
                    });
                }, 'SKU','sku8')->placeholder('模糊查询SKU，名称');
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('skuClassify.season', '季节性')->select(ProductClassify::$_season);
                $filter->equal('sku.classify_id', '品类')->select(ProductClassify::options());
            });
        });
        return $grid;
    }

    public function ajax(){
        $action = request('action');
        $param = request('param');

        $ajax = new PickupOrderAjax();
        return $param ? call_user_func([$ajax,$action],$param) : call_user_func([$ajax,$action]);
    }

    public function modal(){
        $action = request('action');
        $param = request('param');

        $modal = new PickupOrderAjax();
        return $param ? call_user_func([$modal,$action],$param) : call_user_func([$modal,$action]);
    }

    public function edit($id, Content $content){
        $pickupOrder = PickupOrder::query()->findOrFail($id);

        $logs = Logs::getLogs('pickup-order',$id);
        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body(view('product-pickup.pickup-order-edit.index', compact('id','pickupOrder','logs')));
    }

    protected function detail($id){

        $pickupOrder = PickupOrder::query()->findOrFail($id);

        $logs = Logs::getLogs('pickup-order',$id);
        return view('product-pickup.pickup-order-detail.index', compact('id','pickupOrder','logs'));
    }

    public function download(){

        echo "<pre>";
        print_r(111);
        echo "</pre>";
        exit;

        $request = Request::input();
        $query = $this->_initQuery($request);

        //PickupOrder::exeDownload($query);
    }

}
